/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cse308.helper_classes;

import cse308.GUIUtilityClass;
import cse308.General;
import cse308.GeneralEntity;
import cse308.entities.Degreeprogram;
import cse308.entities.Peoattainment;
import cse308.entities.Survey;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.http.Part;

/**
 *
 * @author Aaron
 */
public class SurveyClass implements Serializable, GeneralTest {

    private Map<String, Object> degreePrograms;
    private List<Degreeprogram> allDegreePrograms;
    private List<String> ownDegreePrograms;
    private Map<String, GeneralTest> degreeProgramMap = new HashMap();
    private String groupBelong;
    private String initiator;
    private String semester;
    
    private String fileLocation;
    private File resultsFile;
    private byte[] results;
    private Part resultsFilePart;
    
    private Map<String, Object> peoAttainmentLevel;
    private Integer identifier;
    private String currentPEO;
    private int currentAttainment;

    public SurveyClass(Survey entity) {
        this.identifier = entity.getIdentifier();
        
        this.groupBelong = entity.getGroupBelong();
        this.initiator = entity.getInitiator();
        this.semester = entity.getSemester();
        this.allDegreePrograms = new ArrayList();
        this.ownDegreePrograms = new ArrayList();
        try {
            this.resultsFile = GUIUtilityClass.convertToFile(entity.getResults(), "results" + entity.getIdentifier() + ".txt");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(SurveyClass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SurveyClass.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.fileLocation = "results";
        
        this.degreePrograms = new HashMap();
        this.peoAttainmentLevel = new HashMap();
        for(Degreeprogram dp :entity.getDegreeprogramCollection()){
            degreePrograms.put(dp.getIdentifier(), dp.getIdentifier());
        }
        for(Peoattainment p :entity.getPeoattainmentCollection()){
            peoAttainmentLevel.put(p.getIdpeoattainment(), p.getIdpeoattainment());
        }
        
    }

    public Part getResultsFilePart() {
        return resultsFilePart;
    }
   
   

    @Override
    public String getIdentifier2() {
        return this.groupBelong + String.valueOf(this.identifier);
    }

    @Override
    public ArrayList<Map<String, General>> getMap() {
        ArrayList<Map<String,General>> maps = new ArrayList();
        maps.add((Map)this.degreeProgramMap);
        return maps;
    }

    @Override
    public ArrayList<List<String>> getList() {
        ArrayList<List<String>> list = new ArrayList();
        list.add(this.ownDegreePrograms);
        return list;
    }

    @Override
    public GeneralEntity findCorrespondingEntity() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CSE_308_ABETPU");
        EntityManager em = emf.createEntityManager();
        return em.find(Survey.class, this.identifier);
    }

    @Override
    public boolean equalsEntity(GeneralEntity toEqual) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    //@Override
    public void updateRelationship(GeneralEntity ge1) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   

    public void setResultsFilePart(Part resultsFilePart) {
        this.resultsFilePart = resultsFilePart;
        this.results = GUIUtilityClass.convertToBytesByPart(resultsFilePart);
    }

    public Map<String, Object> getDegreePrograms() {
        return degreePrograms;
    }

    public void setDegreePrograms(Map<String, Object> degreePrograms) {
        this.degreePrograms = degreePrograms;
    }

    public List<Degreeprogram> getAllDegreePrograms() {
        return allDegreePrograms;
    }

    public void setAllDegreePrograms(List<Degreeprogram> allDegreePrograms) {
        this.allDegreePrograms = allDegreePrograms;
    }

    public List<String> getOwnDegreePrograms() {
        return ownDegreePrograms;
    }

    public void setOwnDegreePrograms(List<String> ownDegreePrograms) {
        this.ownDegreePrograms = ownDegreePrograms;
    }

    public Map<String, GeneralTest> getDegreeProgramMap() {
        return degreeProgramMap;
    }

    public void setDegreeProgramMap(Map<String, GeneralTest> degreeProgramMap) {
        this.degreeProgramMap = degreeProgramMap;
    }

    public String getGroupBelong() {
        return groupBelong;
    }

    public void setGroupBelong(String groupBelong) {
        this.groupBelong = groupBelong;
    }

    public String getInitiator() {
        return initiator;
    }

    public void setInitiator(String initiator) {
        this.initiator = initiator;
    }

    public String getSemester() {
        return semester;
    }

    public void setSemester(String semester) {
        this.semester = semester;
    }

    public String getFileLocation() {
        return fileLocation;
    }

    public void setFileLocation(String fileLocation) {
        this.fileLocation = fileLocation;
    }

    public File getResultsFile() {
        return resultsFile;
    }

    public void setResultsFile(File resultsFile) {
        this.resultsFile = resultsFile;
    }

    public byte[] getResults() {
        return results;
    }

    public void setResults(byte[] results) {
        this.results = results;
    }

    public Map<String, Object> getPeoAttainmentLevel() {
        return peoAttainmentLevel;
    }

    public void setPeoAttainmentLevel(Map<String, Object> peoAttainmentLevel) {
        this.peoAttainmentLevel = peoAttainmentLevel;
    }

    public Integer getIdentifier() {
        return identifier;
    }

    public void setIdentifier(Integer identifier) {
        this.identifier = identifier;
    }

    public String getCurrentPEO() {
        return currentPEO;
    }

    public void setCurrentPEO(String currentPEO) {
        this.currentPEO = currentPEO;
    }

    public int getCurrentAttainment() {
        return currentAttainment;
    }

    public void setCurrentAttainment(int currentAttainment) {
        this.currentAttainment = currentAttainment;
    }
    
    
}